package com.yanyilu.basing.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanyilu.basing.common.R;
import com.yanyilu.basing.entity.CountPoint;
import com.yanyilu.basing.mapper.CountPointMapper;
import com.yanyilu.basing.service.ICountPointService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yanyilu.basing.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sxx
 * @since 2020-10-30
 */
@Service
public class CountPointServiceImpl extends ServiceImpl<CountPointMapper, CountPoint> implements ICountPointService {
    @Autowired
    CountPointMapper countPointMapper;

    @Override
    public R addPoint(Integer pointType) {
        int startTime = TimeUtil.intDateTime();
        CountPoint countPoint;
        switch (pointType){
            case 0:
                 countPoint = countPointMapper.selectByTimeAndbtnName(startTime,"活动首页");
                 if(null == countPoint){
                     countPoint = new CountPoint();
                     countPoint.setBtnName("活动首页");
                     countPoint.setDayTime(startTime);
                     countPoint.setPointCount(1);
                     countPointMapper.insert(countPoint);
                 }else{
                     countPoint.setPointCount(countPoint.getPointCount()+1);
                     countPointMapper.updateById(countPoint);
                 }
                 break;
            case 1:
                 countPoint = countPointMapper.selectByTimeAndbtnName(startTime,"活动声明页");
                if(null == countPoint){
                    countPoint = new CountPoint();
                    countPoint.setBtnName("活动声明页");
                    countPoint.setDayTime(startTime);
                    countPoint.setPointCount(1);
                    countPointMapper.insert(countPoint);
                }else{
                    countPoint.setPointCount(countPoint.getPointCount()+1);
                    countPointMapper.updateById(countPoint);
                }
                break;
            case 2:
                 countPoint = countPointMapper.selectByTimeAndbtnName(startTime,"订单详情页");
                if(null == countPoint){
                    countPoint = new CountPoint();
                    countPoint.setBtnName("订单详情页");
                    countPoint.setDayTime(startTime);
                    countPoint.setPointCount(1);
                    countPointMapper.insert(countPoint);
                }else{
                    countPoint.setPointCount(countPoint.getPointCount()+1);
                    countPointMapper.updateById(countPoint);
                }
                break;
            case 3:
                 countPoint = countPointMapper.selectByTimeAndbtnName(startTime,"报名页");
                if(null == countPoint){
                    countPoint = new CountPoint();
                    countPoint.setBtnName("报名页");
                    countPoint.setDayTime(startTime);
                    countPoint.setPointCount(1);
                    countPointMapper.insert(countPoint);
                }else{
                    countPoint.setPointCount(countPoint.getPointCount()+1);
                    countPointMapper.updateById(countPoint);
                }
                break;
            case 4:
                 countPoint = countPointMapper.selectByTimeAndbtnName(startTime,"个人中心页");
                if(null == countPoint){
                    countPoint = new CountPoint();
                    countPoint.setBtnName("个人中心页");
                    countPoint.setDayTime(startTime);
                    countPoint.setPointCount(1);
                    countPointMapper.insert(countPoint);
                }else{
                    countPoint.setPointCount(countPoint.getPointCount()+1);
                    countPointMapper.updateById(countPoint);
                }
                break;
        }
        return R.ok();
    }

    @Override
    public R adminList(Integer page,Integer limit,String startTime,String btnName) {
        PageHelper.startPage(page,limit);
        List<CountPoint> list = countPointMapper.selectAll(startTime,btnName);
        PageInfo<CountPoint> pageInfo = new PageInfo<>(list);
        return R.ok().put("list",pageInfo.getList()).put("count",pageInfo.getTotal()).put("pages",pageInfo.getPages());
    }
}
